$(function () {
    const layer = layui.layer
    const form = layui.form
    getArtCateList();
    function getArtCateList() {
        $.ajax({
            type: 'GET',
            url: '/my/article/cates',
            success: function (res) {
                const htmlSrc = template('tpl-table', res);
                $('tbody').html(htmlSrc)
            }
        })
    }
    let indexAdd = null
    $('#btnAddCate').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '添加文章分类',
            content: $('#dialog-add').html()
        });
    })
    $('body').on('submit', '#form-add', function (e) {
        e.preventDefault()
        $.ajax({
            type: 'POST',
            url: '/my/article/addcates',
            data: $('#form-add').serialize(),
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                layer.msg(res.message)
                layer.close(indexAdd)
                getArtCateList();
            }
        })
    })
    $('#resetAddList').on('click', function (e) {
        e.preventDefault()
        $('#form-add')[0].reset();
    })
    //删除
    $('body').on('click', '#del', function () {
        const id = $(this).parent().siblings('#ID').html();
        layer.confirm('是否确认删除?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                type: 'GET',
                url: `/my/article/deletecate/${id}`,
                success: function (res) {
                    if (res.status !== 0) {
                        return layer.msg(res.message)

                    }
                    layer.msg(res.message)
                    getArtCateList()
                }
            })
            layer.close(index);
        });
    })
    //修改
    let indexRevise = null
    $('body').on('click', '#revise', function () {
        indexRevise = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '添加文章分类',
            content: $('#dialog-revise').html()
        });
        const id = $(this).parent().siblings('#ID').html()
        $.ajax({
            type: 'GET',
            url: `/my/article/cates/${id}`,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                form.val('form-revise', res.data)
            }
        })
    })
    $('body').on('submit', '#form-revise', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: `/my/article/updatecate`,
            data: $(this).serialize(),
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                layer.msg(res.message)
                layer.close(indexRevise)
                getArtCateList();

            }
        })
    })
})